package com.nariano.webapp;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.wicket.Page;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.feedback.ContainerFeedbackMessageFilter;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;

import com.nariano.server.domain.entities.City;
import com.nariano.server.domain.entities.Location;
import com.nariano.server.domain.entities.User;
import com.nariano.server.services.cityservice.CityService;
import com.nariano.server.services.mapservice.MapService;
import com.nariano.server.services.userservice.UserService;
import com.nariano.server.utils.spring.SpringFactory;
import com.nariano.webapp.util.ParameterizedResourceModel;

public class OpponentInteractionPanel extends Panel {

    private User opponent;

    public OpponentInteractionPanel(final User opponent, final User me, String name) {
        super(name);
        
        final CityService cityService = SpringFactory.getBean("cityService", CityService.class);
        final UserService userService = SpringFactory.getBean("userService", UserService.class);

        this.opponent = opponent;

   
        ParameterizedResourceModel opponentInfo = new ParameterizedResourceModel("opponentInfo", getUserName(opponent) );
        
        add(new Label("opponentInfo", opponentInfo));

        add(new Link("gotoMap", null) {

            @Override
            public void onClick() {
            	List<City> cities = cityService.getCities(OpponentInteractionPanel.this.opponent);
            	Location location = cities.isEmpty()?null:cities.iterator().next().getLocation();
                setResponsePage(new WorldMapPage(location));
            }
        });

        add(new Link("attack", null) {

            @Override
            public void onClick() {
            	me.setEnemyUserId(opponent.getId());
            	userService.updateUser(me);
            	setResponsePage(new Index(opponent));
            }
            
            @Override
            public boolean isVisible() {
                return !opponent.equals(me) && 
                	   me.getEnemyUserId()!=opponent.getId();
            }
        });
        
        add(new Link("peace", null) {

            @Override
            public void onClick() {
            	me.setEnemyUserId(null);
            	userService.updateUser(me);
            	setResponsePage(new Index(opponent));
            }
            
            @Override
            public boolean isVisible() {
                return me.getEnemyUserId()==opponent.getId();
            }
        });



    }

	private String getUserName(User opponent) {
		return opponent==null?"-":opponent.getUserName();
	}

}
